/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wonderly.output.log;

import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.openide.modules.ModuleInstall;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.wonderly.log.StreamFormatter;

/**
 * Manages a module's lifecycle. Remember that an installer is optional and
 * often not needed at all.
 */
public class Installer extends ModuleInstall {
	InputOutput io;
	@Override
	public void restored() {
		io = IOProvider.getDefault().getIO("Application Logging", true );
		Handler h = new Handler() {

			@Override
			public void publish(LogRecord record) {
				String str = getFormatter().format( record );
				io.getOut().print(str);
				flush();
			}

			@Override
			public void flush() {
				io.getOut().flush();
			}

			@Override
			public void close() throws SecurityException {
				io.getOut().close();
			}

		};
		h.setFormatter( new StreamFormatter() );
		Logger.getLogger("").addHandler(h);
	}
}
